#include <stdio.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <ctype.h>


#include "comm_tools.h"
#include "t_types.h"

T_INT32 COMM_GetFileSize(T_CHAR *pcFile)
{
	T_INT32 s32Len = -1;
    off_t file_size;
    struct stat stbuf;
    int fd = -1;
	
	if(pcFile)
	{
	      
	    fd = open(pcFile, O_RDONLY);
	    if (fd == -1) 
		{
			printf("open file:%s error\n", pcFile);
			s32Len = -1;
			goto _exit;
	    }
	      
	    if ((fstat(fd, &stbuf) != 0) || (!S_ISREG(stbuf.st_mode))) 
		{
	    	printf("fstat() error\n");
			s32Len = -1;
			goto _exit;
	    }
	      
	    file_size = stbuf.st_size;	
		s32Len = (T_INT32)file_size;
	}
_exit:
	if(fd > 0)
	{
		close(fd);
	}
	return s32Len;
}


T_INT32 COMM_GetTime(COMM_TIME_S *pstTime)
{
	struct tm *tm;
	time_t timtt;
	
	if(!pstTime)
	{
		return -1;
	}

	timtt = time(NULL);

	tm = localtime(&timtt);

	pstTime->unYear = tm->tm_year + 1900;
	pstTime->unMon = tm->tm_mon + 1;
	pstTime->unDayOfMon = tm->tm_mday;
	pstTime->unHour = tm->tm_hour;
	pstTime->unMin = tm->tm_min;
	pstTime->unSec = tm->tm_sec;

	return 0;
}

T_INT32 COMM_GetDateTimeBCD6(T_UINT8 *pDateTime)
{
	COMM_TIME_S stTime;

	COMM_GetTime(&stTime);
	ToBCD2_packet(stTime.unSec, &pDateTime[5]);
	ToBCD2_packet(stTime.unMin, &pDateTime[4]);
	ToBCD2_packet(stTime.unHour, &pDateTime[3]);
	ToBCD2_packet(stTime.unDayOfMon, &pDateTime[2]);
	ToBCD2_packet(stTime.unMon, &pDateTime[1]);
	if((stTime.unYear-2000) < 0)
	{
		pDateTime[0] = 0;
	}
	else
	{
		ToBCD2_packet(stTime.unYear-2000, &pDateTime[0]);
	}
	return 0;
}

T_INT32 COMM_HexStrToByte(const T_CHAR* source, T_UINT8* dest, T_INT32 sourceLen)  
{  
    short i;  
    unsigned char highByte, lowByte;  
      
    for (i = 0; i < sourceLen; i += 2)  
    {  
        highByte = toupper(source[i]);  
        lowByte  = toupper(source[i + 1]);  
  
        if (highByte > 0x39)  
            highByte -= 0x37;  
        else  
            highByte -= 0x30;  
  
        if (lowByte > 0x39)  
            lowByte -= 0x37;  
        else  
            lowByte -= 0x30;  
  
        dest[i / 2] = (highByte << 4) | lowByte;  
    }  
    return ;  
}  

/*

FE 01 -> FF
FE 00 -> FE

*/
T_INT32 COMM_SpecialBinStreamConvert_Decode(T_UINT8 *pu8In, T_INT32 s32InLen, T_UINT8 *pu8Out, T_INT32 *ps32OutLen)
{
	T_INT32 i,j;
	
	if(!pu8In || !pu8Out || !ps32OutLen || s32InLen <= 0)
	{
		return -1;
	}

	for(i=0,j=0; i<(s32InLen);)
	{
		if((pu8In[i] == 0xFE) &&  (pu8In[i+1] == 0x01))
		{
			pu8Out[j] = 0xff;
			i += 2;
			j++;
		}
		else if((pu8In[i] == 0xFE) &&  (pu8In[i+1] == 0x00))
		{
			pu8Out[j] = 0xfe;
			i += 2;
			j++;
		}
		else
		{
			pu8Out[j] = pu8In[i];
			i++;
			j++;
		}
	}

	*ps32OutLen = j;

	return 0;
}

/*
FF -> FE 01
FE -> FE 00

*/
T_INT32 COMM_SpecialBinStreamConvert_Encode(T_UINT8 *pu8In, T_INT32 s32InLen, T_UINT8 *pu8Out, T_INT32 *ps32OutLen)
{
	T_INT32 i,j;
	
	if(!pu8In || !pu8Out || !ps32OutLen || s32InLen <= 0)
	{
		return -1;
	}

	for(i=0,j=0; i<s32InLen;)
	{
		if(pu8In[i] == 0xFF)
		{
			pu8Out[j++] = 0xFE;
			pu8Out[j++] = 0x01;
			i++;
		}
		else if(pu8In[i] == 0xFE)
		{
			pu8Out[j++] = 0xFE;
			pu8Out[j++] = 0x00;
			i++;
		}
		else
		{
			pu8Out[j] = pu8In[i];
			i++;
			j++;
		}
	}

	*ps32OutLen = j;

	return 0;
}

long COMM_HostToNetL(long host)
{
	return htonl(host);
}
long COMM_NetToHostL(long net)
{
	return ntohl(net);
}
unsigned short COMM_HostToNetS(unsigned short host)
{
	return htons(host);
}
unsigned short COMM_NetToHostS(unsigned short net)
{
	return ntohs(net);
}

